<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 15/10/30
 * Time: 下午5:12
 */



class ProductPackItem extends Eloquent
{
    protected $table = "product_pack_item";

    public static function get_package_item($product_id,$car_model_id){
        $product_items = array();
        $work_hours_price = WorkHoursPrice::select(array('price'))->first();
        $product_item = ProductPackItem::where('status',1)
            ->where('pack_id',$product_id)
            ->select('item_id','type')
            ->get()
            ->toArray();
        foreach($product_item as $_item){
            if($_item['type'] == 1){ //配件
                $part_item = ProductPackItem::get_part_item($_item['item_id'],$car_model_id);
                $item = array(
                    'id' => $part_item['id'],
                    'type' => 'part',
                    'name' => $part_item['name'],
                    'desc' => $part_item['name'],
                    'work_hours' => 0,
                    'price' => 0,
                    'item' => $part_item['item']
                );
            }elseif($_item['type'] == 2){ //服务
                $service_item = ServiceProductItem::where('status',1)
                    ->where('id',$_item['item_id'])
                    ->select(array('name','desc','work_hours'))
                    ->first();
                $item = array(
                    'id' => $_item['item_id'],
                    'type' => 'service',
                    'name' => $service_item['name'],
                    'desc' => $service_item['desc'],
                    'work_hours' => $service_item['work_hours'],
                    'price' => $service_item['work_hours'] * $work_hours_price->price, //工时单价*工时
                    'item' => array()
                );
            }
            array_push($product_items,$item);
        }
        return $product_items;
    }

    public static function get_part_item($product_id,$car_model_id){
        $part_subject = SupplySubject::where('status',1)
            ->where('id',$product_id)
            ->select(array('id','name'))
            ->first()->toArray();

        //车型档次
        $car_model_level = DataCarStandardInfo::where('id',$car_model_id)->select(array('model_level'))->first();

        //车型系数
        $car_model_coefficient = WorkModelCoefficient::where('name',$car_model_level->model_level)->select(array('supply_coefficient'))->first();

        //耗材单价 = 车型系数 * 单价
        $item_sql = "select
            c.id,
            d.name,
            c.model,
            c.amount * ". $car_model_coefficient->supply_coefficient ." as amount,
            a.number,
            d.unit,
            a.max_number,
            a.isdefualt,
            c.desc
            from supply_product_item as a , supply_item as b ,supply_item as c ,supply_series as d
            where a.supply_item_id = b.id
            and a.supply_item_id = c.id
            and d.id = c.series_id
            and a.car_model_id = ". $car_model_id ."
            and b.subject = " . $product_id ."
            and a.status = 1
            and b.status = 1
            and c.status = 1
            and d.status = 1;";

        $part_item = DB::select($item_sql);
        $part_items = array(
            'id'  => $part_subject['id'],
            'name' => $part_subject['name'],
            'count' => count($part_item),
            'item' => $part_item
        );
        return $part_items;
    }
}